package com.enniswu.weitter.widget.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.enniswu.weitter.R;
import com.enniswu.weitter.entity.User;
import com.enniswu.weitter.user.info.UserInfoActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.UserViewHolder> {

    private List<User> users;
    private OnItemClickListener clickListener = new SimpleOnItemClickListener();

    public UserAdapter(List<User> users) {
        this.users = users;
    }

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @NonNull
    @Override
    public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_user, parent, false);
        final UserViewHolder holder = new UserViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener == null)
                    return;
                int position = holder.getAdapterPosition();
                clickListener.onItemClick(v, position);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull UserViewHolder holder, int position) {
        User user = users.get(position);
        holder.tvName.setText(user.getName());
        if (TextUtils.isEmpty(user.getSynopsis()))
            holder.tvMsg.setText(R.string.info_synopsis_default);
        else
            holder.tvMsg.setText(user.getSynopsis());
        Glide.with(holder.ivPortrait).load(user.getPortrait())
                .apply(new RequestOptions().circleCrop()).into(holder.ivPortrait);
    }

    @Override
    public int getItemCount() {
        return users.size();
    }

    static class UserViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_portrait)
        ImageView ivPortrait;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_variety)
        TextView tvMsg;

        public UserViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class SimpleOnItemClickListener implements OnItemClickListener {
        @Override
        public void onItemClick(View v, int position) {
            User user = users.get(position);
            if (user != null) {
                Intent intent = new Intent(v.getContext(), UserInfoActivity.class);
                intent.putExtra("user", user);
                v.getContext().startActivity(intent);
            }
        }
    }
}
